package com.ruoyi.quanzi.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.quanzi.mapper.SunTopicMapper;
import com.ruoyi.quanzi.mapper.SunTopicMemberMapper;
import com.ruoyi.quanzi.domain.SunTopic;
import com.ruoyi.quanzi.domain.SunTopicMember;
import com.ruoyi.quanzi.service.ISunTopicMemberService;

/**
 * 话题成员服务实现类
 * 
 * @author ruoyi
 */
@Service
public class SunTopicMemberServiceImpl implements ISunTopicMemberService
{
    @Autowired(required = false)
    private SunTopicMemberMapper sunTopicMemberMapper;
    
    @Autowired(required = false)
    private SunTopicMapper sunTopicMapper;

    /**
     * 查询话题成员
     * 
     * @param id 话题成员ID
     * @return 话题成员
     */
    @Override
    public SunTopicMember selectSunTopicMemberById(Long id)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.selectSunTopicMemberById(id);
        }
        
        // 临时模拟数据
        SunTopicMember member = new SunTopicMember();
        member.setId(id);
        member.setTopicId(1L);
        member.setUid(1L);
        member.setUserName("用户1");
        member.setAvatar("https://example.com/avatar.png");
        member.setRole(0);
        member.setJoinTime(new Date());
        member.setStatus(0);
        member.setCreateTime(new Date());
        return member;
    }

    /**
     * 查询话题成员列表
     * 
     * @param sunTopicMember 话题成员
     * @return 话题成员集合
     */
    @Override
    public List<SunTopicMember> selectSunTopicMemberList(SunTopicMember sunTopicMember)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.selectSunTopicMemberList(sunTopicMember);
        }
        
        // 临时模拟数据
        List<SunTopicMember> list = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            SunTopicMember member = new SunTopicMember();
            member.setId((long)i);
            member.setTopicId(sunTopicMember.getTopicId() != null ? sunTopicMember.getTopicId() : 1L);
            member.setUid((long)i);
            member.setUserName("用户" + i);
            member.setAvatar("https://example.com/avatar" + i + ".png");
            member.setRole(i == 1 ? 1 : 0);
            member.setJoinTime(new Date());
            member.setStatus(0);
            member.setCreateTime(new Date());
            list.add(member);
        }
        return list;
    }

    /**
     * 新增话题成员
     * 
     * @param sunTopicMember 话题成员
     * @return 结果
     */
    @Override
    public int insertSunTopicMember(SunTopicMember sunTopicMember)
    {
        if (sunTopicMemberMapper != null) {
            sunTopicMember.setCreateTime(new Date());
            sunTopicMember.setUpdateTime(new Date());
            int result = sunTopicMemberMapper.insertSunTopicMember(sunTopicMember);
            
            // 更新话题统计信息
            if (result > 0 && sunTopicMapper != null) {
                updateTopicStats(sunTopicMember.getTopicId(), 1, 0, 0);
                updateTopicLastActiveTime(sunTopicMember.getTopicId());
            }
            
            return result;
        }
        return 1;
    }

    /**
     * 修改话题成员
     * 
     * @param sunTopicMember 话题成员
     * @return 结果
     */
    @Override
    public int updateSunTopicMember(SunTopicMember sunTopicMember)
    {
        if (sunTopicMemberMapper != null) {
            sunTopicMember.setUpdateTime(new Date());
            int result = sunTopicMemberMapper.updateSunTopicMember(sunTopicMember);
            
            // 更新话题最后活跃时间
            if (result > 0 && sunTopicMapper != null) {
                updateTopicLastActiveTime(sunTopicMember.getTopicId());
            }
            
            return result;
        }
        return 1;
    }

    /**
     * 批量删除话题成员
     * 
     * @param ids 需要删除的话题成员ID
     * @return 结果
     */
    @Override
    public int deleteSunTopicMemberByIds(Long[] ids)
    {
        if (sunTopicMemberMapper != null) {
            // 查询要删除的成员所属的话题ID
            List<SunTopicMember> members = new ArrayList<>();
            for (Long id : ids) {
                SunTopicMember member = sunTopicMemberMapper.selectSunTopicMemberById(id);
                if (member != null) {
                    members.add(member);
                }
            }
            
            int result = sunTopicMemberMapper.deleteSunTopicMemberByIds(ids);
            
            // 更新话题统计信息
            if (result > 0 && sunTopicMapper != null && !members.isEmpty()) {
                for (SunTopicMember member : members) {
                    updateTopicStats(member.getTopicId(), -1, 0, 0);
                }
            }
            
            return result;
        }
        return ids.length;
    }

    /**
     * 删除话题成员信息
     * 
     * @param id 话题成员ID
     * @return 结果
     */
    @Override
    public int deleteSunTopicMemberById(Long id)
    {
        if (sunTopicMemberMapper != null) {
            // 查询要删除的成员所属的话题ID
            SunTopicMember member = sunTopicMemberMapper.selectSunTopicMemberById(id);
            
            int result = sunTopicMemberMapper.deleteSunTopicMemberById(id);
            
            // 更新话题统计信息
            if (result > 0 && sunTopicMapper != null && member != null) {
                updateTopicStats(member.getTopicId(), -1, 0, 0);
            }
            
            return result;
        }
        return 1;
    }
    
    /**
     * 查询话题成员
     * 
     * @param topicId 话题ID
     * @param userId 用户ID
     * @return 话题成员
     */
    @Override
    public SunTopicMember selectSunTopicMemberByTopicIdAndUserId(Long topicId, Long userId)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.selectMemberByTopicAndUser(topicId, userId);
        }
        
        // 临时模拟数据
        SunTopicMember member = new SunTopicMember();
        member.setId(1L);
        member.setTopicId(topicId);
        member.setUid(userId);
        member.setUserName("用户" + userId);
        member.setAvatar("https://example.com/avatar.png");
        member.setRole(0);
        member.setJoinTime(new Date());
        member.setStatus(0);
        member.setCreateTime(new Date());
        return member;
    }
    
    /**
     * 用户加入话题
     * 
     * @param userId 用户ID
     * @param topicId 话题ID
     * @return 结果
     */
    @Override
    public int joinTopic(Long userId, Long topicId)
    {
        if (sunTopicMemberMapper != null) {
            // 创建新的话题成员记录
            SunTopicMember member = new SunTopicMember();
            member.setTopicId(topicId);
            member.setUid(userId);
            member.setRole(0); // 普通成员
            member.setStatus(0); // 正常状态
            member.setJoinTime(new Date());
            member.setLastActiveTime(new Date());
            member.setPostCount(0);
            member.setIntegral(0);
            member.setJoinType(0); // 主动加入
            member.setCreateTime(new Date());
            member.setUpdateTime(new Date());
            
            return sunTopicMemberMapper.insertSunTopicMember(member);
        }
        return 1;
    }
    
    /**
     * 用户退出话题
     * 
     * @param userId 用户ID
     * @param topicId 话题ID
     * @return 结果
     */
    @Override
    public int leaveTopic(Long userId, Long topicId)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.deleteMemberByTopicAndUser(topicId, userId);
        }
        return 1;
    }
    
    /**
     * 根据话题ID查询成员列表
     * 
     * @param topicId 话题ID
     * @return 成员集合
     */
    @Override
    public List<SunTopicMember> selectMembersByTopicId(Long topicId)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.selectTopicMembers(topicId, null, 0); // 查询正常状态的成员
        }
        
        // 临时模拟数据
        SunTopicMember queryMember = new SunTopicMember();
        queryMember.setTopicId(topicId);
        return selectSunTopicMemberList(queryMember);
    }
    
    /**
     * 根据用户ID查询加入的话题列表
     * 
     * @param userId 用户ID
     * @return 话题成员集合
     */
    @Override
    public List<SunTopicMember> selectTopicsByUserId(Long userId)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.selectUserTopics(userId, 0); // 查询正常状态的话题
        }
        
        // 临时模拟数据
        SunTopicMember queryMember = new SunTopicMember();
        queryMember.setUid(userId);
        return selectSunTopicMemberList(queryMember);
    }
    
    /**
     * 检查用户是否为话题成员
     * 
     * @param userId 用户ID
     * @param topicId 话题ID
     * @return 是否为成员
     */
    @Override
    public boolean isUserMemberOfTopic(Long userId, Long topicId)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.checkUserJoined(topicId, userId);
        }
        return false;
    }
    
    /**
     * 获取话题成员数量
     * 
     * @param topicId 话题ID
     * @return 成员数量
     */
    @Override
    public int getMemberCountByTopicId(Long topicId)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.countTopicMembers(topicId, 0); // 统计正常状态的成员
        }
        return 15; // 模拟数据
    }
    
    /**
     * 设置成员角色
     * 
     * @param memberId 成员ID
     * @param role 角色
     * @return 结果
     */
    @Override
    public int setMemberRole(Long memberId, String role)
    {
        if (sunTopicMemberMapper != null) {
            SunTopicMember member = sunTopicMemberMapper.selectSunTopicMemberById(memberId);
            if (member != null) {
                member.setRole(Integer.parseInt(role));
                member.setUpdateTime(new Date());
                return sunTopicMemberMapper.updateSunTopicMember(member);
            }
        }
        return 1;
    }
    
    /**
     * 根据话题ID查询管理员列表
     * 
     * @param topicId 话题ID
     * @return 管理员集合
     */
    @Override
    public List<SunTopicMember> selectAdminsByTopicId(Long topicId)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.selectTopicAdmins(topicId);
        }
        
        // 临时模拟数据
        List<SunTopicMember> list = new ArrayList<>();
        SunTopicMember admin = new SunTopicMember();
        admin.setId(1L);
        admin.setTopicId(topicId);
        admin.setUid(1L);
        admin.setUserName("管理员");
        admin.setAvatar("https://example.com/admin.png");
        admin.setRole(1);
        admin.setJoinTime(new Date());
        admin.setStatus(0);
        admin.setCreateTime(new Date());
        list.add(admin);
        return list;
    }
    
    
    /**
     * 统计话题成员数量
     * 
     * @param topicId 话题ID
     * @return 成员数量
     */
    @Override
    public int countTopicMembers(Long topicId)
    {
        if (sunTopicMemberMapper != null) {
            return sunTopicMemberMapper.countTopicMembers(topicId, 0);
        }
        return 10;
    }
    
    /**
     * 设置成员角色
     * 
     * @param id 成员ID
     * @param role 角色（0普通成员 1管理员 2创建者）
     * @return 结果
     */
    @Override
    public int setMemberRole(Long id, Integer role)
    {
        if (sunTopicMemberMapper != null) {
            // 查询成员信息
            SunTopicMember member = sunTopicMemberMapper.selectSunTopicMemberById(id);
            if (member != null) {
                // 更新角色
                member.setRole(role);
                member.setUpdateTime(new Date());
                return sunTopicMemberMapper.updateSunTopicMember(member);
            }
        }
        return 1;
    }
    
    /**
     * 解除成员禁言
     * 
     * @param id 成员ID
     * @return 结果
     */
    @Override
    public int unmuteMember(Long id)
    {
        if (sunTopicMemberMapper != null) {
            // 查询成员信息
            SunTopicMember member = sunTopicMemberMapper.selectSunTopicMemberById(id);
            if (member != null) {
                // 更新状态
                member.setStatus(0);
                member.setMuteEndTime(null);
                member.setUpdateTime(new Date());
                return sunTopicMemberMapper.updateSunTopicMember(member);
            }
        }
        return 1;
    }
    
    /**
     * 更新话题统计信息
     * 
     * @param topicId 话题ID
     * @param memberDelta 成员数变化
     * @param postDelta 帖子数变化
     * @param viewDelta 浏览数变化
     * @return 结果
     */
    private int updateTopicStats(Long topicId, int memberDelta, int postDelta, int viewDelta)
    {
        if (sunTopicMapper != null) {
            // 查询话题信息
            SunTopic topic = sunTopicMapper.selectSunTopicById(topicId);
            if (topic != null) {
                // 更新统计信息
                topic.setMemberCount(topic.getMemberCount() + memberDelta);
                topic.setPostCount(topic.getPostCount() + postDelta);
                topic.setViewCount(topic.getViewCount() + viewDelta);
                topic.setUpdateTime(new Date());
                
                // 更新话题
                return sunTopicMapper.updateSunTopic(topic);
            }
        }
        return 0;
    }
    
    /**
     * 更新话题最后活跃时间
     * 
     * @param topicId 话题ID
     * @return 结果
     */
    private int updateTopicLastActiveTime(Long topicId)
    {
        if (sunTopicMapper != null) {
            // 查询话题信息
            SunTopic topic = sunTopicMapper.selectSunTopicById(topicId);
            if (topic != null) {
                // 更新最后活跃时间
                topic.setLastActiveTime(new Date());
                topic.setUpdateTime(new Date());
                
                // 更新话题
                return sunTopicMapper.updateSunTopic(topic);
            }
        }
        return 0;
    }
}